<template>
  <div>
    <!-- 彩色展示框 -->
    <div class="container-fluid">
      <el-row class="spannel_list" :gutter="10">
        <el-col :sm="6" :xs="12">
          <div class="spannel">
            <em>10015</em><span>部</span>
            <b>库存手机数量</b>
          </div>
        </el-col>
        <el-col :sm="6" :xs="12">
          <div class="spannel scolor01">
            <em>123</em><span>部</span>
            <b>今日已售出数量</b>
          </div>
        </el-col>
        <el-col :sm="6" :xs="12">
          <div class="spannel scolor02">
            <em>7067</em><span>个</span>
            <b>用户总数</b>
          </div>
        </el-col>
        <el-col :sm="6" :xs="12">
          <div class="spannel scolor03">
            <em>123</em><span>个</span>
            <b>日新增用户数量</b>
          </div>
        </el-col>
      </el-row>
    </div>
    <!-- 折线图 -->
    <div class="container-fluid">
      <el-row class="stackedLine">
        <el-card style="height: 260px">
          <div style="height: 240px" ref="stackedLineShow"></div>
        </el-card>
      </el-row>
    </div>
    <!-- 表格展示区 -->
    <el-card style="margin-top: 20px">
      <el-table :data="tableData">
        <el-table-column v-for="(val, key) in tableLabel" :key="key" :prop="key" :label="val"> </el-table-column>
      </el-table>
    </el-card>
    <!-- 柱状图和饼状图 -->
    <div class="container-fluid">
      <el-row class="curve-pie" :gutter="10">
        <div class="graph">
          <el-card style="height: 260px" class="chart1">
            <div style="height: 240px" ref="userecharts"></div>
          </el-card>
          <el-card style="height: 260px" class="chart2">
            <div style="height: 240px" ref="videoecharts"></div>
          </el-card>
        </div>
      </el-row>
    </div>
    
  </div>
</template>

<script>
import { getData } from '@/api/data.js'
import * as echarts from 'echarts'

export default {
  name: 'Home',
  data() {
    return {
      tableData: [],
      tableLabel: {
        name: '手机型号',
        todayBuy: '今日购买',
        monthBuy: '本月购买',
        totalBuy: '总购买'
      }
    }
  },
  mounted() {
    getData().then(res => {
      const { code, data } = res.data
      if (code === 20000) {
        //表格展示区
        this.tableData = data.tableData
        const order = data.orderData
        // 饼状图
        const videoOption = {
          title: {
            text: '品牌比例',
            left: 'center'
          },
          series: [
            {
              type: 'pie',
              data: data.videoData,
              radius: '60%'
            }
          ],
          tooltip: {
            trigger: 'item'
          }
        }
        const V = echarts.init(this.$refs.videoecharts)
        V.setOption(videoOption)

        //柱状图
        const userOption = {
          legend: {
            textStyle: {
              color: '#333'
            }
          },
          grid: {
            left: '20%'
          },
          tooltip: {
            trigger: 'axis'
          },
          xAxis: {
            type: 'category',
            data: data.userData.map(item => item.date),
            axisLine: {
              lineStyle: {
                color: '#17b3a3'
              }
            },
            axisLabel: {
              interval: 0,
              color: '#333'
            }
          },
          yAxis: [
            {
              type: 'value',
              axisLine: {
                lineStyle: {
                  color: '#17b3a3'
                }
              }
            }
          ],
          series: [
            {
              name: '新增用户',
              type: 'bar',
              data: data.userData.map(item => item.new)
            },
            {
              name: '活跃用户',
              type: 'bar',
              data: data.userData.map(item => item.active)
            }
          ]
        }
        const U = echarts.init(this.$refs.userecharts)
        U.setOption(userOption)
      }

      //折线图
      const option = {
        title: {
          text: '季度营业额'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['iphone', '华为', 'onePlus', '三星']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: data.staData.xdata
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '华为',
            type: 'line',
            stack: 'Total',
            data: [220, 182, 91, 234]
          },
          {
            name: 'onePlus',
            type: 'line',
            stack: 'Total',
            data: [150, 232, 101, 154]
          },
          {
            name: '三星',
            type: 'line',
            stack: 'Total',
            data: [320, 408, 101, 334]
          },
          {
            name: 'iphone',
            type: 'line',
            stack: 'Total',
            data: [120, 132, 1010, 134]
          }
        ]
      }
      const U = echarts.init(this.$refs.stackedLineShow)
      U.setOption(option)
    })
  }
}
</script>

<style lang="less" scoped>
.spannel_list {
  margin-top: 20px;

  .spannel {
    height: 100px;
    overflow: hidden;
    text-align: center;
    position: relative;
    background-color: #fff;
    border: 1px solid #e7e7e9;
    margin-bottom: 20px;
  }

  .spannel em {
    font-style: normal;
    font-size: 50px;
    line-height: 50px;
    display: inline-block;
    margin: 10px 0 0 20px;
    font-family: 'Arial';
    color: #83a2ed;
  }

  .spannel span {
    font-size: 14px;
    display: inline-block;
    color: #83a2ed;
    margin-left: 10px;
  }

  .spannel b {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    line-height: 24px;
    background: #e5e5e5;
    color: #333;
    font-size: 14px;
    font-weight: normal;
  }

  .scolor01 em,
  .scolor01 span {
    color: #6ac6e2;
  }

  .scolor02 em,
  .scolor02 span {
    color: #5fd9de;
  }

  .scolor03 em,
  .scolor03 span {
    color: #58d88e;
  }

  .gragh_pannel {
    height: 350px;
    border: 1px solid #e7e7e9;
    background-color: #fff !important;
    margin-bottom: 20px;
  }

  .column_pannel {
    margin-bottom: 20px;
    height: 400px;
    border: 1px solid #e7e7e9;
    background-color: #fff !important;
  }
}
.graph {
  display: flex;
  .chart1 {
    width: 60%;
  }
  .chart2 {
    width: 40%;
  }
}
</style>
